package Common;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class CodeBase {
	//testing
	static final String yahooURL_1 = "http://boss.yahooapis.com/ysearch/web/v1/";
	static final String yahooURL_2 = "?appid=";
	static final String yahooURL_3 = "&format=xml&abstract=long&sites=";
	
	public Node root;
	
	public HashMap<String, String>NodeToFileMap;
	
	public String yahooID;
	
	public ArrayList<String> keywords;
	public String domain;	
	
    public String formQuery()
    {
		StringBuilder sb = new StringBuilder(yahooURL_1);
		for(String s : keywords)
		{
			sb.append(s+"%20");
		}
		sb.append(yahooURL_2);
		sb.append(yahooID);
		sb.append(yahooURL_3);
		sb.append(domain);
		String query = sb.toString();
		
		return query;
    }

	public CodeBase(String yahooID,
			ArrayList<String> keywords, String domain) {
		super();
		this.yahooID = yahooID;
		this.keywords = keywords;
		this.domain = domain;
		this.root = new Node("root");
		this.NodeToFileMap = new HashMap<String, String>();
		parseHierarchy("hierarchy.txt");
	}
	
	public void parseHierarchy(String hierarchyFile)
	{
		try {
			FileReader fileReader = new FileReader(hierarchyFile);
			BufferedReader reader = new BufferedReader(fileReader);			
			String newLine;
			while((newLine = reader.readLine()) != null)
			{
				String[] substrings = newLine.split("#");
				NodeToFileMap.put(substrings[0], substrings[1]);
			}				
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	public class Node {
		public String label;
		public String file;
		public ArrayList<Node> childNodes;
		
		public boolean isLeaf()
		{
			return file.equals("NONE");
		}

		public Node(String label) {
			this.label = label;
			this.childNodes = new ArrayList<Node>();
			this.file = NodeToFileMap.get(label);
		}
		
	}
	
}


